package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Company;
import com.example.demo.entity.JobPosition;
import com.example.demo.entity.User;
import com.example.demo.service.CompanyService;
import com.example.demo.service.JobPositionService;
import com.example.demo.service.UserService;
import com.example.demo.vo.CompanyVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 公司表(Company)表控制层
 *
 * @author makejava
 * @since 2023-12-13 08:40:32
 */
@RestController
@RequestMapping("company")
public class CompanyController {
    /**
     * 服务对象
     */
    @Autowired
    private CompanyService companyService;

    @Autowired
    private JobPositionService jobPositionService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param current 当前
     * @param size    数量
     * @return 所有数据
     */
    @GetMapping("list")
    public Result list(Integer current, Integer size) {
        IPage<Company> list=companyService.selectPage(new  Page<>(current,size));
        return Result.success(list);
    }

    /**
     * 通过用户Id查询单条数据
     *
     * @param id 用户Id
     * @return 单条数据
     */
    @GetMapping("selectById")
    public List<Company> selectById(@RequestParam Integer id) {
        return companyService.selectByUserId(id);
    }


    /**
     * 创建用户插入公司信息
     *
     * @param companyVo 用户信息公司信息
     * @return {@code String}
     */
    @PostMapping("doAdd")
    public Result doAdd(@RequestBody CompanyVo companyVo) {
        try {
            if (companyVo.getAccount()!=null){
                QueryWrapper<User> wrapper=new QueryWrapper<>();
                wrapper.eq("account",companyVo.getAccount());
                if (userService.getOne(wrapper) != null){
                    return Result.error("账号重复");
                }
                User user=new User();
                user.setAccount(companyVo.getAccount());
                user.setPassword(companyVo.getPassword());
                user.setPhone(companyVo.getPhone());
                userService.save(user);
                Company company=getCompany(companyVo);
                companyService.save(company);
                return Result.success("企业添加成功");
            }else {
                Company company=getCompany(companyVo);
                company.setId(companyVo.getId());
                companyService.save(company);
                return Result.success("企业修改成功");
            }
        }catch (Exception e){
            return Result.error("数据错误");
        }
    }

    private Company getCompany(CompanyVo companyVo){
        Company company=new Company();
        company.setCompanyLogo(companyVo.getCompanyLogo());
        company.setCompanyName(companyVo.getCompanyName());
        company.setIndustry(companyVo.getIndustry());
        company.setCompanyProperty(companyVo.getCompanyProperty());
        company.setCompanySize(companyVo.getCompanySize());
        company.setFinancing(companyVo.getFinancing());
        company.setCompanyAddress(companyVo.getCompanyAddress());
        company.setCompanyDescription(companyVo.getCompanyDescription());
        company.setRecruitingPositions(companyVo.getRecruitingPositions());
        company.setHrName(companyVo.getHrName());
        company.setUserId(companyVo.getUserId());
        return company;
    }




    /**
     * 上传logo
     *
     * @param file 文件
     * @param id   公司id
     * @return {@code Result}
     */
    @PostMapping("uploadLogo")
    public Result uploadLogo(@RequestParam MultipartFile file, @RequestParam Integer id) {
        // 检查图片是否为空
        if (file.isEmpty()) {
            return Result.error("400", "请选择要上传的图片");
        }
        try {
            // 获取图片信息
            byte[] data = file.getBytes();
            Company company = companyService.getById(id);
            company.setCompanyLogo(data);
            UpdateWrapper<Company> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id);
            companyService.update(company, wrapper);
            // 创建ImageInfo对象并保存到数据库
            return Result.success("图片上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("图片上传失败");
        }
    }

    /**
     * 删除公司信息
     *
     * @param id 公司id
     * @return {@code Result}
     */
    @DeleteMapping("delete")
    public Result delete(Integer id) {
        try {
            companyService.removeById(id);
            QueryWrapper<JobPosition> wrapper=new QueryWrapper<>();
            wrapper.eq("companyId",id);
            if (jobPositionService.list(wrapper)!=null ){
                jobPositionService.remove(wrapper);
            }
            return Result.success("删除成功");
        }catch (Exception e){
            return Result.error("删除失败");
        }
    }

}

